import React, { Component } from 'react'

import './index.css'
export default class index extends Component {

    handleKeyUp = (event) => {
        const { target, key } = event
        if (key !== 'Enter') return
        if (target.value.trim() === '') {
            alert('请输入内容')
            return
        }
        //创建一个todo对象
        const todoObj = { id: (new Date()).valueOf(), name: target.value, done: false }
        //通过调用父函数的方法实现子向父传递数据
        this.props.addTodo(todoObj)
        target.value = ''
    }
    render() {
        return (
            <div className="todo-header">
                <input type="text" placeholder="请输入你的任务名称，按回车键确认" onKeyUp={this.handleKeyUp} />
            </div>
        )
    }
}
